﻿/*
操作代码需要统一编码。目前明确的编码有：
登入：LOGON_ACT 登出：LOGOUT_ACT 一般操作：NORMAL_ACT 考勤登入：CHECK_IN_ACT 考勤登出:CHECK_OUT_ACT
 * 
 * 
 * */
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Text;
using System.Xml;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using EQT.Model;

namespace EQT.Dal
{
    public partial class DaActLog
    {

        private static readonly string SQL_INSERT_ACT_LOG = "INSERT INTO Act_Log (UID ,ACT_CODE ,ACT_Time ,ACT_INFO ) VALUES "
            + "(@Uid,@Act_Code,getdate(),@Act_Info)";
        private static DbCommand dbAct_Log_InsertCommand;
        private static readonly Database logDb ;

        /// <summary>
        /// 总是在静态构造函数中，初始化静态成员
        /// </summary>
        static DaActLog()
        {
            logDb = DatabaseFactory.CreateDatabase();
            dbAct_Log_InsertCommand = logDb.GetSqlStringCommand(SQL_INSERT_ACT_LOG);
            //设置参数:Uid	
            logDb.AddInParameter(dbAct_Log_InsertCommand, "Uid", DbType.AnsiString);
            //设置参数:Act_Code	
            logDb.AddInParameter(dbAct_Log_InsertCommand, "Act_Code", DbType.AnsiString);
            //设置参数:Act_Time	
            //db.AddInParameter(dbCommand, "Act_Time", DbType.DateTime, DateTime.Now);
            //设置参数:Act_Info	
            logDb.AddInParameter(dbAct_Log_InsertCommand, "Act_Info", DbType.String);
        }

        /// <summary>
        /// 活动日志。
        /// </summary>
        /// <param name="UID"></param>
        /// <param name="Act_Code">登入：LOGON_ACT 登出：LOGOUT_ACT 一般操作：NORMAL_ACT 考勤登入：CHECK_IN_ACT 考勤登出:CHECK_OUT_ACT</param>
        /// <param name="Act_Info">附加信息</param>
        /// <returns></returns>
        public static int LogOperation(string UID, string Act_Code, string Act_Info)
        {
            if (Act_Info.Length > 200) Act_Info = Act_Info.Substring(0, 200);

            logDb.SetParameterValue(dbAct_Log_InsertCommand, "Uid", UID);
            logDb.SetParameterValue(dbAct_Log_InsertCommand, "Act_Code", Act_Code);
            logDb.SetParameterValue(dbAct_Log_InsertCommand, "Act_Info", Act_Info);
            try
            {
                return logDb.ExecuteNonQuery(dbAct_Log_InsertCommand);

            }
            catch (DbException ex)
            {
                HandleDbException(ex);
                return -1;
            }
        }

        /// <summary>
        /// 考勤登入操作日志登记。
        /// </summary>
        /// <param name="UID"></param>
        /// <param name="Act_Code"></param>
        /// <param name="Act_Info"></param>
        /// <returns></returns>
        public static int LogCheckIn(string UID, string Act_Info)
        {
            
            return LogOperation(UID, "CHECK_IN_ACT", "考勤登入：" + Act_Info);
        }

        public static int LogNormalAct(string UID, string Act_Info)
        {
            return LogOperation(UID, "NORMAL_ACT", Act_Info);
        }

        /// <summary>
        /// 查询考勤记录分页方法
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="TotalCount"></param>
        /// <returns></returns>
        public DataSet GetTimeRecords(string condition, int PageSize, int PageIndex, out int TotalCount)
        {
            string strGetActLogRecords = "select * from VMonth_TimeBook ";

            if (!string.IsNullOrEmpty(condition))
            {
                strGetActLogRecords = strGetActLogRecords + condition;
            }

            try
            {
                return DaCommon.GetPaginationData(strGetActLogRecords, PageSize, PageIndex, " UID desc", out TotalCount);

            }
            catch (DbException ex)
            {
                HandleDbException(ex);
            }
            TotalCount = 0;
            return null;
        }

       

        /// <summary>
        /// 查询日志记录分页方法
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="TotalCount"></param>
        /// <returns></returns>
        public DataSet GetActLogRecords(string condition, int PageSize, int PageIndex, out int TotalCount)
        {            
            string strGetActLogRecords = "SELECT * from V_System_Log ";

            if (!string.IsNullOrEmpty(condition))
            {
                strGetActLogRecords = strGetActLogRecords + condition;
            }

            try
            {
                return DaCommon.GetPaginationData(strGetActLogRecords, PageSize, PageIndex, "log_id desc,ACT_Time desc", out TotalCount);

            }
            catch (DbException ex)
            {
                HandleDbException(ex);
            }
            TotalCount = 0;
            return null;
        }


        public DataSet GetTimeRec(string sequenceKey)
        {
            //usp_GetTimeBookByDeptPerMonth    @yearmonth varchar(8),@dept_id varchar(50)
            //usp_GetTimeBookByMonth           @yearmonth varchar(8)
            //usp_GetTimeBookByUserPerMonth    @yearmonth varchar(8),@UID varchar(50)
            //usp_GetTimeBookByUserPerYear     @year varchar(4),@UID varchar(50)


            return db.ExecuteDataSet("DBO.USP_SeqNextValue", new object[] { sequenceKey, "", "" });
            
        }

        /// <summary>
        /// 查询某月某部门的全部考勤数据.
        /// </summary>
        /// <param name="yearmonth"></param>
        /// <param name="deptid"></param>
        /// <returns></returns>
        public DataSet GetTimeBookByDeptPerMonth(string yearmonth, string deptid)
        {

            DataSet ds = db.ExecuteDataSet("DBO.usp_GetTimeBookByDeptPerMonth", new object[] { yearmonth, deptid });
            return ds;
        }

        /// <summary>
        /// 查询某月某人的考勤记录,应该是一条.
        /// </summary>
        /// <param name="yearmonth"></param>
        /// <param name="uid"></param>
        /// <returns></returns>
        public DataSet GetTimeBookByUserPerMonth(string yearmonth, string uid)
        {
            
            DataSet ds = db.ExecuteDataSet("DBO.usp_GetTimeBookByUserPerMonth", new object[] { yearmonth, uid });
            return ds;
        }

        /// <summary>
        /// 查询某月的全部考勤数据。
        /// </summary>
        /// <param name="yearmonth"></param>
        /// <returns></returns>
        public DataSet GetTimeBookByMonth(string yearmonth)
        {
           
            DataSet ds = db.ExecuteDataSet("DBO.usp_GetTimeBookByMonth", new object[] { yearmonth });
            return ds;
        }

        /// <summary>
        /// 查询某人某年的全部考勤数据.
        /// </summary>
        /// <param name="year"></param>
        /// <param name="uid"></param>
        /// <returns></returns>
        public DataSet GetTimeBookByUserPerYear(string year, string uid)
        {
            
            DataSet ds = db.ExecuteDataSet("DBO.usp_GetTimeBookByUserPerYear", new object[] { year, uid });
            return ds;
        }
    }
}

